//
//  PKReadDetailTableViewCell.m
//  PianKe
//
//  Created by ybq on 15/12/29.
//  Copyright © 2015年 B7_Young. All rights reserved.
//

#import "PKReadDetailTableViewCell.h"
@implementation PKReadDetailTableViewCell

- (void)awakeFromNib {
    // Initialization code
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self creatUI];
    }
    return self;
}

- (void)creatUI {
    _title = [[UILabel alloc] init];
    _title.font = [UIFont systemFontOfSize:20];
    _title.numberOfLines = 0;
    [self.contentView addSubview:_title];
    
    _mainImageView = [[UIImageView alloc] init];
    [self.contentView addSubview:_mainImageView];
    
    _content = [[UILabel alloc] init];
    _content.textColor = [UIColor grayColor];
    _content.font = [UIFont systemFontOfSize:14];
    _content.numberOfLines = 2;
    
    [self.contentView addSubview:_content];
}

- (void)setModel:(PKReadDetailModel *)model {
    _model = model;
    _title.text = model.title;
    [_title setFrame:CGRectMake(20, 20, kWidth - 40, [self getLabelHeightByString:_title])];
    
    [_mainImageView setFrame:CGRectMake(20, _title.frame.size.height + 40, 130, 60)];
    [_mainImageView sd_setImageWithURL:[NSURL URLWithString:model.coverimg] placeholderImage:[UIImage imageNamed:@"readPlaceHolder"]];
    
    _content.text = model.content;
    [_content setFrame:CGRectMake(155, _mainImageView.frame.origin.y, 195, [self getLabelHeightByString:_content])];

}

- (CGFloat)getLabelHeightByString:(UILabel *)label {
    NSString *string = label.text;
    NSDictionary *attributes = nil;
    CGSize size = CGSizeZero;
    if ([label isEqual:_title]) {
        attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:20]};
        size = CGSizeMake(kWidth - 40, 1000);
    } else {
        attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:14]};
        size = CGSizeMake(195, 1000);
    }
    
    CGSize contentactually = [string boundingRectWithSize:size options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading) attributes:attributes context:nil].size;
    return contentactually.height;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
